

/*document.onselectstart = function() {
  return false;
};
*/

define(['utils/appFunc'],function(appFunc){
	 var $$ = Dom7;
	
		

var rand = function(rMi, rMa){return ~~((Math.random()*(rMa-rMi+1))+rMi);}
var orbs = [];
var Timer;
var radius;
var canvas;
var ctx;
var finder = {
	
	
init : function(id) {
	
	//canvas = document.createElement('canvas');
    //ctx = canvas.getContext('2d');
	
canvas= document.getElementById('ccc');
ctx = canvas.getContext('2d');
canvas.width = cw = window.innerWidth;
canvas.height = ch = window.innerHeight;
ctx.lineCap = 'round';

	//ctx.fillStyle = 'rgba(0,0,0,.1)';
	//ctx.fillRect(0,0,cw,ch);
	//
		/*base64Str = canvas.toDataURL('image/jpeg', 0.4);
	$$("div#ccc").find('img').attr('src', base64Str);
	*/
	//alert( $$("div#"+id).find('img').attr('src') );
	//$$("#"+id).html(base64Str);
	
	//console.log(base64Str);
	var count = 300;
	while(count--){
			finder.createOrb(cw/2, ch/2+(count*1));
	}
	
	finder.loop();
},
	
	
	
createOrb : function(mx,my){
	
  var dx = (cw/2) - mx;
	var dy = (ch/2) - my;
	var dist = Math.sqrt(dx * dx + dy * dy);
	var angle = Math.atan2(dy, dx);
	orbs.push({
		x: mx,
		y: my,
		lastX: mx,
		lastY: my,
		hue: 0,
		colorAngle: 0,
		angle: angle + Math.PI/2,
		//size: .5+dist/250,
		size: rand(1,3)/2,
		centerX: cw/2,
		centerY: ch/2,		
		radius: dist,
		speed: 0.01,
		//speed: (rand(5,10)/1000)*(dist/750)+.015,
		alpha: 1 - Math.abs(dist)/cw,
		draw: function() {			
			ctx.strokeStyle = 'hsla('+this.colorAngle+',100%,50%,1)';	
			ctx.lineWidth = this.size;			
			ctx.beginPath();
			ctx.moveTo(this.lastX, this.lastY);
			ctx.lineTo(this.x, this.y);
			ctx.stroke();
		},	
		update: function(){
			var mx = this.x;
			var my = this.y;	
			this.lastX = this.x;
			this.lastY = this.y;
			var x1 = cw/2;
			var y1 = ch/2;
			var x2 = mx;
			var y2 = my;		
			var rise = y1-y2;
			var run = x1-x2;
			var slope = -(rise/run);
			var radian = Math.atan(slope);
			var angleH = Math.floor(radian*(180/Math.PI));		
			if(x2 < x1 && y2 < y1){angleH += 180;}		
			if(x2 < x1 && y2 > y1){angleH += 180;}		
			if(x2 > x1 && y2 > y1){angleH += 360;}		
			if(y2 < y1 && slope =='-Infinity'){angleH = 90;}		
			if(y2 > y1 && slope =='Infinity'){angleH = 270;}		
			if(x2 < x1 && slope =='0'){angleH = 180;}
			if(isNaN(angleH)){angleH = 0;}
			
			this.colorAngle = angleH;
			this.x = this.centerX + Math.sin(this.angle*-1) * this.radius;
			this.y = this.centerY + Math.cos(this.angle*-1) * this.radius;
			this.angle += this.speed;
		
		}
	});
	
},
clear:function(){
 orbs = []; 
 clearTimeout(Timer);
},

loop: function(){
	
Timer = window.setTimeout(finder.loop, 1E3 / 60);

var img=document.getElementById("lamp");
var pat=ctx.createPattern(img,"no-repeat");
ctx.fillStyle=pat;
ctx.globalAlpha=0.1;

	//ctx.fillStyle = 'rgba(0,255,255,0.1)';
	ctx.fillRect(0,0,cw,ch);
	var i =orbs.length;
	while(i--){	
		var orb = orbs[i];	
		var updateCount = 2;
		while(updateCount--){
		orb.update();		
		orb.draw(ctx);
		}
		
	}
}



}


return finder;

});
            

